package kr.bos.Subclasses;

import java.io.File;
import java.util.ArrayList;

import kr.bos.R;
import kr.bos.Framework.BaseActivity;
import kr.bos.Framework.Logger;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
import android.widget.Toast;

/**
 * SD카드에 dir 생성. 에뮬 작동 위함. 
 * @author baek
 */
public class FolderGenToSD extends BaseActivity{

	ArrayList<String> strDN = new ArrayList<String>();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.single_text);

		//추가하고자하는 DN
		strDN.add("testDN1");
		strDN.add("testDN2");
//		strDN.add("cn=금상2,ou=테스트지점,ou=테스트회사,ou=테스트업종,o=SignKorea,c=KR");
//		strDN.add("cn=김영진-10189567,ou=HTS,ou=굿모닝신한,ou=증권,o=SignKorea,c=KR");
//		strDN.add("cn=윤희대,ou=HTS,ou=굿모닝신한,ou=증권,o=SignKorea,c=KR");
//		strDN.add("cn=이태웅,ou=WTS,ou=교보,ou=증권,o=SignKorea,c=KR");
		
		String str = Environment.getExternalStorageState();
		String path = ""+Environment.getExternalStorageDirectory();
		Logger.d("ExternalStoragePath = "+path);
		String txtmsg = "";
		if ( str.equals(Environment.MEDIA_MOUNTED)) {
			Logger.v("SD 인식 성공. Mounted found");
			txtmsg = "SD 인식 성공. Mounted found\n";

			for(int i = 0 ; i < strDN.size() ; i++){
				String dirPath = path+"/NPKI/SignKorea/USER/"+strDN.get(i);
				Logger.d("DIR["+(i+1)+"]"+dirPath);
				File file = new File(dirPath); 
				if( !file.isDirectory() ){  //없으면 폴더 만듦.
					file.mkdirs();//가운데 모든경로 생성//유효
//					file.mkdir();//그 폴더만 생성
					Logger.d("Creating Dir..");
					txtmsg+="Creating Dir..("+(i+1)+")\n";
				}else{
					Logger.d("Dir Already Exists");
					txtmsg+="Dir Already Exists("+(i+1)+")\n";
				}
			}
			
			//TRY rename ( adb shell에서 한글입력불가..")
			File file = new File(path+"/NPKI/SignKorea/USER/"+strDN.get(0));//testdn1
			File toFile = new File(path+"/NPKI/SignKorea/USER/"+"cn=김영진-10189567,ou=HTS,ou=굿모닝신한,ou=증권,o=SignKorea,c=KR");
			file.renameTo(toFile);
			if(toFile.isDirectory()){
				txtmsg += "이름바꾸기 성공";
			}else{
				txtmsg += "이름바꾸기 실패";
			}

		}else{
			Toast.makeText(this, "SD Card 인식 실패", Toast.LENGTH_SHORT).show();
			txtmsg = "SD Card 인식 실패";
		}
		
		((TextView)findViewById(R.id.textView1)).setText("END OF PROCESS\n"+txtmsg);
	}
}

